#!/bin/sh -e

patch -p1 < no-dbus.patch

# Build autoconf 2.13 for Firefox's sole use.
# See: https://bugzilla.mozilla.org/show_bug.cgi?id=104642
(
    cd autoconf2.13

    ./configure \
        --prefix="$PWD/../junk" \
        --program-suffix=-2.13

    make
    make install
)

# Build yasm for Firefox's sole use.
# Firefox is the only package which needs it
# and upstream is kinda dead.
(
    cd yasm

    ./configure \
        --prefix="$PWD/../junk"

    make
    make install
)
export PATH="$PWD/junk/bin:$PATH"

mkdir -p build
cd build

# Bypass 'ccache' as it's totally useless when building
# Firefox and only slows things down.
export CC="${CC:-/usr/bin/cc}"
export CXX="${CXX:-/usr/bin/c++}"

export LDFLAGS="$LDFLAGS -Wl,-rpath=/usr/lib/firefox"
export RUSTFLAGS="$RUSTFLAGS -Cdebuginfo=0"
export MOZ_DEBUG_FLAGS=-g0
export MOZ_NOSPAM=1

../configure \
    --prefix=/usr \
    --libdir=/usr/lib \
    --enable-official-branding \
    --enable-optimize="$CFLAGS -w" \
    --enable-install-strip \
    --enable-strip \
    --enable-rust-simd \
    --enable-application=browser \
    --enable-release \
    --enable-alsa \
    --without-system-nspr \
    --without-system-nss \
    --with-system-jpeg \
    --with-system-zlib \
    --with-system-png \
    --without-system-libvpx \
    --with-system-pixman \
    --with-system-ffi \
    --disable-profiling \
    --disable-accessibility \
    --disable-tests \
    --disable-system-extension-dirs \
    --disable-parental-controls \
    --disable-debug-symbols \
    --disable-callgrind \
    --disable-vtune \
    --disable-elf-hack \
    --disable-gold \
    --disable-jemalloc \
    --disable-pulseaudio \
    --disable-crashreporter \
    --disable-updater \
    --disable-dbus \
    --disable-eme \
    --disable-necko-wifi

make
make DESTDIR="$1" install

# Remove a lot of uneeded "stuff".
rm -rf "$1/usr/include"
rm -rf "$1/usr/lib/firefox-devel"
rm -rf "$1/usr/share/idl"
rm -rf "$1/usr/lib/firefox/gtk2" # Drop GTK+2 runtime dependency.
rm -f  "$1/usr/lib/firefox/browser/features/fxmonitor@mozilla.org.xpi"
rm -f  "$1/usr/lib/firefox/browser/features/screenshots@mozilla.org.xpi"
rm -f  "$1/usr/lib/firefox/browser/features/webcompat-reporter@mozilla.org.xpi"
rm -f  "$1/usr/lib/firefox/browser/features/webcompat@mozilla.org.xpi"
rm -f  "$1/usr/lib/firefox/browser/features/doh-rollout@mozilla.org.xpi"
